﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///S_City 的摘要说明
/// </summary>
public  partial class S_City
{
    public static S_City GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.S_City.FirstOrDefault(row => row.CityID == id);
        }
    }

    public static List<S_City> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.S_City.ToList();
        }
    }

    public static List<S_City> GetAllByCondition(int ProID,string key)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from c in context.S_City
                        select c;
            if (ProID != 0)
                query = query.Where(row => row.ProvinceID == ProID);
            if (!string.IsNullOrEmpty(key))
                query = query.Where(row => row.CityName.Contains(key));
            return query.ToList();
        }
    }


    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.S_City.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<S_City>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.S_City.FirstOrDefault(row => row.CityID == id);
            if (objItem == null)
                return;
            context.S_City.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }



}